package com.lsa.helpers.tree;

public class LeafCountingWalker<T> extends TreeWalker<T>
{
    public LeafCountingWalker()
    {
        super( new LeafCountingVisitor<T>() );
    }
    
    public int getLeafCount(){
        return ((LeafCountingVisitor<T>)visitor).getLeafCount();
    }
    
    public void reset(){
        ((LeafCountingVisitor<T>)visitor).reset();
    }
    
    private static class LeafCountingVisitor<T> implements ITreeVisitor<T>{
        private int leafCount = 0;
        
        public void reset(){
            leafCount = 0;
        }
        
        public int getLeafCount()
        {
            return leafCount;
        }

        @Override
        public void visit(ITreeNode<T> node) throws Exception{
            if (node.getChildren().size() == 0){
                ++leafCount;
            }
        }
    }
}
